package rankList;
import java.io.*;
import java.util.*;
import java.io.UnsupportedEncodingException;


public class RankList {

    /**
     * 负责人：confident  ------->>>   思路很好值得学习
     * 通过文件读取用户数据
     * 把积分的由小到大进行排序
     * 打印出用户的排名、用户名、积分数
     */
    static class User {
        String username;
        int score;
        public User(String username, int score) {
            this.username = username;
            this.score = score;
        }
    }


    public RankList(){
        viewRank();
    }

    public void viewRank() {
        List<User> users = new ArrayList<>();
        String filePath = "File\\ranking.txt"; // 假设数据存储在 ranking.txt
        // 读取文件数据
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(":"); // 假设数据格式：username,score
                if (parts.length == 2) {
                    String username = parts[0].trim();
                    int score = Integer.parseInt(parts[1].trim());
                    users.add(new User(username, score));
                }
            }
        } catch (IOException e) {
            System.err.println("❌ 文件读取失败: " + e.getMessage());
            return;
        }
        // 如果没有用户数据，则提示
       if (users.isEmpty()) {
           System.out.println("当前没有排名数据");
            return;
       }
        System.out.println("🏆🏆🏆🏆🏆排行榜🏆🏆🏆🏆🏆");
        System.out.println("排名\t 用户名\t 积分");
        for (int i = 0; i < users.size();i++)
        {
            try {//对齐操作
                if(users.get(i).username.getBytes("UTF-8").length<3)
                {
                    System.out.printf("%d\t %s\t\t %d\n", i + 1, users.get(i).username, users.get(i).score);
                }
                else {
                    System.out.printf("%d\t %s\t %d\n", i + 1, users.get(i).username, users.get(i).score);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        System.out.println();
        System.out.println("0:返回主菜单");
        Scanner scanner = new Scanner(System.in);
        int choice;
        while(true) {
            System.out.println("输入你的选择:");
            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
                break;
            } else {
                System.out.println("非法输入,请重新输入:");
                scanner.next();
            }
        }
    }
}

